package com.sright.bcard.common.biz.manager.impl;

import com.alibaba.fastjson.JSON;
import com.sright.bcard.common.biz.manager.TranscodeManager;
import com.sright.bcard.common.share.bo.FileTranscodeBO;
import com.sright.bcard.common.service.share.enums.MaterialTypeSimpleEnum;
import com.sright.bcard.content.dal.dao.MaterialDAO;
import com.sright.bcard.content.dal.dao.TranscodeLogDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Component
@Slf4j
public class TranscodeManagerImpl implements TranscodeManager {
    @Value("${TRANSCODE_URL}")
    private String transcodeUrl = "116.62.85.47:9703";

    @Value("${TRANSCODE_CALLBACK_URL}")
    private String callbackUrl = "https://bcard-miniapp-dev-console.ergengtv.com/common/callback.json";

    @Autowired
    private MaterialDAO materialDAO;

    @Autowired
    private TranscodeLogDAO transcodeLogDAO;

    @Override
    public Boolean materialTranscode(Long materialId, String filePath, Integer materialType) {
        FileTranscodeBO fileTranscodeBO = new FileTranscodeBO();
        //根据素材类型选择转码素材
        String templateId;
        if (materialType == 4 || materialType == 8) {
            //横视频转码模板
            templateId = "572224cb71e84684fd0f787aeb910921";
        } else if (materialType == 1 || materialType == 6) {
            //竖视频转码模板
            templateId = "a61c33cee10247908ac0fe1144b4358c";
        } else {
            return true;
        }

        Map<String, Object> template = new HashMap<>();
        template.put("TemplateId", templateId);
        template.put("UserData", new HashMap<String, String>() {{
            put("materialId", String.valueOf(materialId));
            put("type", MaterialTypeSimpleEnum.getName(materialType));
        }});
        List<Map<String, Object>> templateList = new ArrayList<>();
        templateList.add(template);

        List list = new LinkedList();
        list.add("6404263");
        list.add(JSON.toJSONString(templateList));
        list.add(callbackUrl);
        list.add("ergeng-video");
        list.add(filePath);
        fileTranscodeBO.setParams(list);
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.postForObject("http://" + transcodeUrl,fileTranscodeBO,String.class);
        log.info("视频上传转码结果 result : {}", result);
        return true;
    }
}
